package com.huluobo.a2205parkdemo.news

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205parkdemo.R
import com.huluobo.a2205parkdemo.adapter.NewsAdapter
import com.huluobo.a2205parkdemo.adapter.NewsPagingAdapter
import com.huluobo.a2205parkdemo.databinding.ActivityNewsBinding
import com.huluobo.base.bean.NewsBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.parse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
        private lateinit var newsAdapter: NewsAdapter
//    private val newsPagingAdapter = NewsPagingAdapter()
    private var page = 0
    override fun initData() {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_PAGE] = page

        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        vm.getAllNews(body)

        vm.geAllNewsSuccess.observe(this) {
            val list = Gson().fromJson(it, NewsBean::class.java)
            newsAdapter.data.clear()
            newsAdapter.data.addAll(list)
            newsAdapter.notifyDataSetChanged()
        }

        vm.addNewsSuccess.observe(this) {
            Log.i("TagA", "添加新闻成功:$it")
            binding.newsPageNumTv.text = "第${page + 1}页"
            vm.getAllNews(body)
        }
    }

    override fun initView() {
        binding.newsHeader.headerTitle.text = "新闻"
        binding.newsHeader.headerBack.visibility = View.VISIBLE
        binding.newsHeader.headerBack.setOnClickListener { finish() }
        binding.newsHeader.headerAdd.visibility = View.VISIBLE
        binding.newsHeader.headerAdd.setOnClickListener {
            //添加新闻弹窗
            val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_news, null)

            AlertDialog.Builder(this)
                .setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        val newsTitle = dialogView.findViewById<EditText>(R.id.add_news_title_et).text.toString()
                        val newsValue = dialogView.findViewById<EditText>(R.id.add_news_value_et).text.toString()
                        val newsTime = getTime()
                        var tagId = 0
                        when {
                            dialogView.findViewById<RadioButton>(R.id.add_news_wuye).isChecked -> tagId = 0

                            dialogView.findViewById<RadioButton>(R.id.add_news_fun).isChecked -> tagId = 1

                            dialogView.findViewById<RadioButton>(R.id.add_news_sport).isChecked -> tagId = 2

                            dialogView.findViewById<RadioButton>(R.id.add_news_shehui).isChecked -> tagId = 3

                            dialogView.findViewById<RadioButton>(R.id.add_news_morning).isChecked -> tagId = 4
                        }

                        if (newsTitle.isNotEmpty() && newsValue.isNotEmpty() && newsTime.isNotEmpty()) {
                            //调用添加新闻方法
                            val map = mutableMapOf<String, Any>()
                            map[Const.PARAM_NEWS_TITLE] = newsTitle
                            map[Const.PARAM_NEWS_VALUE] = newsValue
                            map[Const.PARAM_NEWS_TIME] = newsTime
                            map[Const.PARAM_NEWS_TAG_ID] = tagId

                            val json = Gson().toJson(map)
                            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                            vm.addNews(body)
                        } else {
                            ToastUtils.showLong("新闻标题或者内容不能为空")
                        }

                    }
                })
                .show()
        }

        binding.newsRv.layoutManager = LinearLayoutManager(this)
        newsAdapter = NewsAdapter()
        binding.newsRv.adapter = newsAdapter

        binding.newsPageNumTv.text = "第${page + 1}页"


        binding.prePage.setOnClickListener {
            if (page == 0) {
                ToastUtils.showLong("已经是第一页了")
            } else {
                page--
                binding.newsPageNumTv.text = "第${page + 1}页"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_PAGE] = page

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getAllNews(body)
            }
        }

        binding.nextPage.setOnClickListener {
            page++
            binding.newsPageNumTv.text = "第${page + 1}页"
            val map = mutableMapOf<String, Int>()
            map[Const.PARAM_PAGE] = page

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            vm.getAllNews(body)
        }

//        binding.newsRv.layoutManager = LinearLayoutManager(this)
//        binding.newsRv.adapter = newsPagingAdapter
//
//        lifecycleScope.launch {
//            vm.getNewsPagingData().collect {
//                newsPagingAdapter.submitData(it)
//            }
//        }
    }
}